﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Data
{
    public sealed class DemSo
    {
        private static readonly string[] StrSo = { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
        private static readonly string[] StrDonViNho = { "linh", "lăm", "mười", "mươi", "mốt", "trăm" };
        private static readonly string[] StrDonViLon = { "", "nghìn", "triệu", "tỷ" };
        private static string[] _strMainGroup;
        private static string[] _strSubGroup;

        public  static string DemTongTien(string strSend)
        {
            bool boKtNull = false;
            string strKq = "";
            string strA = strSend.Trim();
            int iIndex = strA.Length - 9;
            int iPreIndex = 0;

            if (strSend.Trim() == "")
            {
                return Len1("0");
            }
            //tra ve khong neu la khong
            for (int i = 0; i < strA.Length; i++)
            {
                if (strA.Substring(i, 1) != "0")
                {
                    break;
                }
                if (i == strA.Length - 1)
                {
                    return StrSo[0];
                }
            }
            int k = 0;
            while (strSend.Trim().Substring(k++, 1) == "0")
            {
                strA = strA.Remove(0, 1);
            }
            //
            if (strA.Length < 9)
            {
                iPreIndex = strA.Length;
            }
            //
            if ((strA.Length % 9) != 0)
            {
                _strMainGroup = new string[strA.Length / 9 + 1];
            }
            else
            {
                _strMainGroup = new string[strA.Length / 9];
            }
            //nguoc
            for (int i = _strMainGroup.Length - 1; i >= 0; i--)
            {
                if (iIndex >= 0)
                {
                    iPreIndex = iIndex;
                    _strMainGroup[i] = strA.Substring(iIndex, 9);
                    iIndex -= 9;
                }
                else
                {
                    _strMainGroup[i] = strA.Substring(0, iPreIndex);
                }

            }
            /////////////////////////////////
            //tach moi maingroup thanh nhieu subgroup
            //xuoi
            for (int j = 0; j < _strMainGroup.Length; j++)
            {
                //gan lai gia tri
                iIndex = _strMainGroup[j].Length - 3;
                if (_strMainGroup[j].Length < 3)
                {
                    iPreIndex = _strMainGroup[j].Length;
                }
                if ((_strMainGroup[j].Length % 3) != 0)
                {
                    _strSubGroup = new string[_strMainGroup[j].Length / 3 + 1];
                }
                else
                {
                    _strSubGroup = new string[_strMainGroup[j].Length / 3];
                }
                for (int i = _strSubGroup.Length - 1; i >= 0; i--)
                {
                    if (iIndex >= 0)
                    {
                        iPreIndex = iIndex;
                        _strSubGroup[i] = _strMainGroup[j].Substring(iIndex, 3);
                        iIndex -= 3;
                    }
                    else
                    {
                        _strSubGroup[i] = _strMainGroup[j].Substring(0, iPreIndex);
                    }
                }
                //duyet subgroup de lay string
                for (int i = 0; i < _strSubGroup.Length; i++)
                {
                    boKtNull = false;//phai de o day
                    if ((j == _strMainGroup.Length - 1) && (i == _strSubGroup.Length - 1))
                    {
                        if (_strSubGroup[i].Length < 3)
                        {
                            if (_strSubGroup[i].Length == 1)
                            {
                                strKq += Len1(_strSubGroup[i]);
                            }
                            else
                            {
                                strKq += Len2(_strSubGroup[i]);
                            }
                        }
                        else
                        {
                            strKq += Len3(_strSubGroup[i]);
                        }
                    }
                    else
                    {
                        if (_strSubGroup[i].Length < 3)
                        {
                            if (_strSubGroup[i].Length == 1)
                            {
                                strKq += Len1(_strSubGroup[i]) + " ";
                            }
                            else
                            {
                                strKq += Len2(_strSubGroup[i]) + " ";
                            }
                        }
                        else
                        {
                            if (Len3(_strSubGroup[i]) == null)
                            {
                                boKtNull = true;
                            }
                            else
                            {
                                strKq += Len3(_strSubGroup[i]) + " ";
                            }
                        }
                    }
                    //dung
                    if (!boKtNull)
                    {
                        if (_strSubGroup.Length - 1 - i != 0)
                        {
                            strKq += StrDonViLon[_strSubGroup.Length - 1 - i] + ", ";
                        }
                        else
                        {
                            strKq += StrDonViLon[_strSubGroup.Length - 1 - i] + " ";
                        }

                    }
                }
                //dung
                if (j != _strMainGroup.Length - 1)
                {
                    if (!boKtNull)
                    {
                        strKq = strKq.Substring(0, strKq.Length - 1) + StrDonViLon[3] + ", ";
                    }
                    else
                    {
                        strKq = strKq.Substring(0, strKq.Length - 1) + " " + StrDonViLon[3] + ", ";
                    }
                }
            }
            //xoa ky tu trang
            strKq = strKq.Trim();
            //xoa dau , neu co
            if (strKq.Substring(strKq.Length - 1, 1) == ".")
            {
                strKq = strKq.Remove(strKq.Length - 1, 1);
            }
            return strKq;
        }
        private static string Len1(string strA)
        {
            return StrSo[int.Parse(strA)];
        }
        private static string Len2(string strA)
        {
            if (strA.Substring(0, 1) == "0")
            {
                return StrDonViNho[0] + " " + Len1(strA.Substring(1, 1));
            }
            if (strA.Substring(0, 1) == "1")
            {
                if (strA.Substring(1, 1) == "5")
                {
                    return StrDonViNho[2] + " " + StrDonViNho[1];
                }
                if (strA.Substring(1, 1) == "0")
                {
                    return StrDonViNho[2];
                }
                return StrDonViNho[2] + " " + Len1(strA.Substring(1, 1));
            }

            if (strA.Substring(1, 1) == "5")
            {
                return Len1(strA.Substring(0, 1)) + " " + StrDonViNho[3] + " " + StrDonViNho[1];
            }
            if (strA.Substring(1, 1) == "0")
            {
                return Len1(strA.Substring(0, 1)) + " " + StrDonViNho[3];
            }
            if (strA.Substring(1, 1) == "1")
            {
                return Len1(strA.Substring(0, 1)) + " " + StrDonViNho[3] + " " + StrDonViNho[4];
            }
            return Len1(strA.Substring(0, 1)) + " " + StrDonViNho[3] + " " + Len1(strA.Substring(1, 1));
        }
        private static string Len3(string strA)
        {
            if ((strA.Substring(0, 3) == "000"))
            {
                return null;
            }
            if ((strA.Substring(1, 2) == "00"))
            {
                return Len1(strA.Substring(0, 1)) + " " + StrDonViNho[5];
            }
            return Len1(strA.Substring(0, 1)) + " " + StrDonViNho[5] + " " + Len2(strA.Substring(1, strA.Length - 1));
        }
    }
}
